package com.game.jinhua;

import com.game.common.QueueEventThread;
import com.game.message.Msg;
import com.game.message.connetion.ConnectionManager;
import com.game.message.messagecode.GameServerEnum;
import com.game.message.task.IAsynProcessor;
import com.game.message.task.IProcessor;
import com.game.message.task.ProcessorManager;
import com.game.message.task.ProcessorQueueEvent;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

/**
 * Created by legend9999 on 2016/6/19.
 */
@Component
public class JinHuaBiz {
	private static Logger logger = LoggerFactory.getLogger(JinHuaBiz.class);
	@Autowired
	private ProcessorManager processorManager;
	@Autowired
	private ApplicationContext applicationContext;
	@Autowired
	private QueueEventThread queueEventThread;

	public void onMsg(Msg msg, ChannelHandlerContext ctx) throws Exception {
		if (msg.getSID() != GameServerEnum.JINHUA.id) {
			return;
		}
		int msgId = msg.getMsgId();
		IProcessor processor = processorManager.getProcess(msgId);
		//调用本地processor
		if (processor instanceof IAsynProcessor) {
			try {
				processor.process(msg, ctx);
			} catch (Exception e) {
				logger.error("IAsynProcessor {} error ", processor);
				logger.error("IAsynProcessor", e);
			}
		} else {
			ProcessorQueueEvent bean = applicationContext.getBean(ProcessorQueueEvent.class);
			bean.setCtx(ctx);
			bean.setRequest(msg);
			bean.setProcessor(processor);
			queueEventThread.addProcessorTask(bean);
		}
	}
}
